// package com.ifp.opengate.boot.filter;
//
// import java.net.InetAddress;
// import java.net.UnknownHostException;
// import java.util.Map;
// import java.util.concurrent.ConcurrentHashMap;
//
// import org.apache.commons.collections.CollectionUtils;
// import org.apache.logging.log4j.ThreadContext;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.cloud.gateway.filter.GatewayFilterChain;
// import org.springframework.cloud.gateway.filter.GlobalFilter;
// import org.springframework.cloud.gateway.route.Route;
// import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
// import org.springframework.core.Ordered;
// import org.springframework.http.server.reactive.ServerHttpRequest;
// import org.springframework.web.server.ServerWebExchange;
//
// import com.ifp.core.log.Trace;
// import com.ifp.opengate.boot.bean.access.IPBlackAndWhiteBean;
// import com.ifp.opengate.boot.constants.Constants;
// import com.ifp.opengate.boot.constants.Event;
// import com.ifp.opengate.boot.emum.ErrorEnum;
// import com.ifp.opengate.boot.exception.AccessControlException;
// import com.ifp.opengate.boot.task.ConnectorCacheDataTask;
// import com.ifp.opengate.boot.utils.RequestUtil;
// import com.ifp.opengate.boot.utils.SeqUtil;
//
// import reactor.core.publisher.Mono;
//
/// **
// * Created by mogs on 2019/4/19. ip黑名单拦截器
// */
// public class IPBlacklistFilter implements GlobalFilter, Ordered {
//
// @Autowired
// IPBlackAndWhiteBean ipBlackAndWhiteBean;
//
// @Autowired
// com.ifp.opengate.boot.utils.RouteBlackBeanUtils RouteBlackBeanUtils;
//
// @Autowired
// ConnectorCacheDataTask connectorCacheDataTask;
//
// @Autowired
// SeqUtil seqUtil;
//
// @Override
// public Mono<Void> filter(ServerWebExchange serverWebExchange, GatewayFilterChain gatewayFilterChain) {
// String clientIp = "";
// try {
// InetAddress ia = InetAddress.getLocalHost();
// clientIp = ia.getHostAddress();
// } catch (UnknownHostException e) {
// clientIp = "127.0.0.1";
// }
// String traceId = seqUtil.getTraceId(clientIp);
// ThreadContext.put("traceId", traceId);
// serverWebExchange.getAttributes().put("traceId", traceId);
// String requestIP = deployCatGetReqIP(serverWebExchange);
// serverWebExchange.getAttributes().put(Constants.REQUEST_IP, requestIP);
// Route route = serverWebExchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
// String grbId = Event.ROUTEBLACK + route.getId();
// // TODO 定位是否有值
// if (!this.ipBlackAndWhiteBean.getIpaddrblackMap().isEmpty()) {
// Trace.log("IPBlacklistInterceptor", 1,
// "getIpAddress(HttpServletRequest) - X-Forwarded-For - String requestIP={}", new Object[] {requestIP});
// if (this.inBlacklist(requestIP, this.ipBlackAndWhiteBean.getIpaddrblackMap())) {
// Trace.log("INTERCEPTOR", 2, "request fail with blackMap filter. ip: {}", new Object[] {requestIP});
// throw new AccessControlException(ErrorEnum.accessIPError.code(),
// String.format(ErrorEnum.accessIPError.msg()));
// }
// } else {
// Trace.log(Trace.MODULE_COMMON, Trace.LEVEL_INFO, "blackMap is empty!!!");
// }
// if (CollectionUtils.isNotEmpty(this.ipBlackAndWhiteBean.getBlackList())) {
// Trace.log(Trace.MODULE_COMMON, 1,
// "getIpAddress(HttpServletRequest) - X-Forwarded-For - =String requestIP={}", new Object[] {requestIP});
// if (this.ipBlackAndWhiteBean.getBlackList().contains(requestIP)) {
// Trace.log("INTERCEPTOR", 2, "request fail with blacklist filter。 ip: {}", new Object[] {requestIP});
// throw new AccessControlException(ErrorEnum.accessIPError.code(),
// String.format(ErrorEnum.accessIPError.msg()));
// }
// } else {
// Trace.log(Trace.MODULE_COMMON, Trace.LEVEL_INFO, "blacklist is empty!!");
// }
//
// if (!RouteBlackBeanUtils.getRouteBlackParamMap().isEmpty()) {
// Trace.log("RouteBlacklistInterceptor", 1, "routeBlack-->grbId", grbId);
// if (this.inRouteBlackMap(grbId, RouteBlackBeanUtils.getRouteBlackParamMap())) {
// Trace.log("INTERCEPTOR", 2, "request fail with routeblacklist filter. grbId: {}", "");
// throw new AccessControlException(ErrorEnum.accessRouteBlackError.code(),
// String.format(ErrorEnum.accessRouteBlackError.msg()));
// }
// } else {
// Trace.log(Trace.MODULE_COMMON, Trace.LEVEL_INFO, "routeblacklist is empty!!");
// }
//
// return gatewayFilterChain.filter(serverWebExchange);
// }
//
// private String deployCatGetReqIP(ServerWebExchange serverWebExchange) {
// String requestIP = this.getIpAddress(serverWebExchange.getRequest());
// // try {
// // List<CatTransation> catTransations = (List<CatTransation>)
// // serverWebExchange.getAttributes().get(Constants.CAT_TRANSACTION);
// // if (catTransations !=null && !catTransations.isEmpty()) {
// // CatTransation transation = new CatTransation();
// // transation.setStatus("0");
// // transation.setDesc("requestIp[" + requestIP + "]:ServerIp[" + connectorCacheDataTask.getIp() + "]");
// // transation.setTransationName("IP.Info");
// // catTransations.add(transation);
// // }
// // } catch (Exception ex){
// // throw new BaseException(ErrorEnum.accessIPError.code(),ErrorEnum.accessIPError.msg());
// // }
// return requestIP;
// }
//
// private boolean inBlacklist(String ip, Map<Object, Object> ipmap) {
// if (ipmap.containsKey(ip)) {
// // 状态为0是启用
// if (ipmap.get(ip).equals("0")) {
// return true;
// }
// }
// return false;
// }
//
// private boolean inRouteBlackMap(String grbId, ConcurrentHashMap<Object, Object> rbmap) {
// if (rbmap.containsKey(grbId)) {
// // 状态为0是启用
// if (rbmap.get(grbId).equals("0")) {
// return true;
// }
// }
// return false;
// }
//
// private String getIpAddress(ServerHttpRequest request) {
// String ip = RequestUtil.getHeaderValue(request, "X-Forwarded-For");
// Trace.log("FILTER", 1, "getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip={}", new Object[] {ip});
// if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
// Trace.log("FILTER", 1, "代理IP:{}", new Object[] {ip});
// if (ip.length() > 15) {
// String[] ips = ip.split(",");
//
// for (int index = 0; index < ips.length; ++index) {
// String strIp = ips[index];
// if (!"unknown".equalsIgnoreCase(strIp)) {
// ip = strIp;
// break;
// }
// }
// }
// } else {
// if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
// ip = RequestUtil.getHeaderValue(request, "Proxy-Client-IP");
// Trace.log("FILTER", 0, "1.getIpAddress(HttpServletRequest) - Proxy-Client-IP - String ip={}",
// new Object[] {ip});
// }
//
// if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
// ip = RequestUtil.getHeaderValue(request, "WL-Proxy-Client-IP");
// Trace.log("FILTER", 0, "2.getIpAddress(HttpServletRequest) - WL-Proxy-Client-IP - String ip={}",
// new Object[] {ip});
// }
//
// if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
// ip = RequestUtil.getHeaderValue(request, "HTTP_CLIENT_IP");
// Trace.log("FILTER", 0, "3.getIpAddress(HttpServletRequest) - HTTP_CLIENT_IP - String ip={}",
// new Object[] {ip});
// }
//
// if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
// ip = RequestUtil.getHeaderValue(request, "HTTP_X_FORWARDED_FOR");
// Trace.log("FILTER", 0, "4.getIpAddress(HttpServletRequest) - HTTP_X_FORWARDED_FOR - String ip={}",
// new Object[] {ip});
// }
//
// if (request.getRemoteAddress() != null) {
// ip = request.getRemoteAddress().getHostString();
// Trace.log("FILTER", 0, "5.getRemoteAddress - String ip={}", new Object[] {ip});
// }
// if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
// ip = request.getURI().getHost();
// Trace.log("FILTER", 0, "6.getIpAddress(HttpServletRequest) - getRemoteAddr - String ip={}",
// new Object[] {ip});
// }
// }
//
// return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
// }
//
// @Override
// public int getOrder() {
// return Ordered.HIGHEST_PRECEDENCE;
// }
// }
